package com.ywgt.controller;

import com.github.pagehelper.Page;
import com.ywgt.pojo.Address;
import com.ywgt.service.AddressService;
import com.ywgt.util.ResponseUtil;
import io.swagger.annotations.ApiModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/admin/address")
@ApiModel("地址管理")
public class AddressController {

    @Autowired
    AddressService addressService;

    /**
     *page: 1,
     *limit: 20,
     *name: undefined,
     *userId: undefined,
     *sort: 'add_time',
     * order: 'desc'
     * @return
     */
    @GetMapping("/list")
    public Object getAddresses(String name, Integer userId, @RequestParam(required = false,defaultValue = "add_time") String sort,
                               @RequestParam(required = false,defaultValue = "desc") String order,
                               @RequestParam(required = false,defaultValue = "1") int page,
                               @RequestParam(required = false,defaultValue = "10") int limit){
        System.out.println(addressService);
        Page<Address> addresses=addressService.getAddresses(name,userId,sort,order,page,limit);
        List<Address> items = addresses.getResult();
        long total = addresses.getTotal();
        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", items);
        return ResponseUtil.ok(data);
    }
}
